﻿using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using OxyPlot;
using OxyPlot.Axes;
using LinearAxis = OxyPlot.Axes.LinearAxis;
using LineSeries = OxyPlot.Series.LineSeries;

namespace APC_Application.Pages
{
    /// <summary>
    /// Interaction logic for FinishPage.xaml
    /// </summary>
    public partial class FinishPage : UserControl, ISwitchable
    {
        public PlotModel PlotModel { get; private set; }

        public FinishPage()
        {
            InitializeComponent();
            DataContext = this;

            PlotModel = new PlotModel("Data for træningspas");
            var linearAxis1 = new LinearAxis();
            PlotModel.Axes.Add(linearAxis1);
            var linearAxis2 = new LinearAxis();
            linearAxis2.Position = AxisPosition.Bottom;
            PlotModel.Axes.Add(linearAxis2);
        }

        public void UtilizeState(object state)
        {
            if (state == null)
                GenerateTestData();

            var arg = (ListsModel) state;

            if (arg == null)
                return;

            GenerateLiveData(arg.pulse.ToList(), "Puls data", OxyColors.Green);
            GenerateLiveData(arg.saturation.ToList(), "Iltmætnings data", OxyColors.Red);
        }

        private void GenerateLiveData(List<int> list, string title, OxyColor color)
        {
            var ls = new LineSeries();
            ls.Color = color;
            ls.MarkerFill = color;
            ls.Title = title;
            for (var i = 0; i < list.Count(); i++)
            {
                ls.Points.Add(new DataPoint(i, list.ElementAt(i)));
            }

            PlotModel.Series.Add(ls);
            //PlotModel.RefreshPlot(true);
        }

        private void GenerateTestData()
        {
            var ls1 = new LineSeries();
            //ls1.Color = OxyColor.FromArgb(255, 78, 154, 6);
            ls1.Color = OxyColors.Green;
            ls1.MarkerFill = OxyColors.Green;
            ls1.Title = "Puls data";

            ls1.Points.Add(new DataPoint(0, 3));
            ls1.Points.Add(new DataPoint(1, 6));
            ls1.Points.Add(new DataPoint(2, 2));
            ls1.Points.Add(new DataPoint(3, 8));
            ls1.Points.Add(new DataPoint(4, 6));
            ls1.Points.Add(new DataPoint(5, 1));
            ls1.Points.Add(new DataPoint(6, 5));
            ls1.Points.Add(new DataPoint(7, 2));
            ls1.Points.Add(new DataPoint(8, 3));
            ls1.Points.Add(new DataPoint(9, 6));

            PlotModel.Series.Add(ls1);

            var ls2 = new LineSeries();
            //ls2.Color = OxyColor.FromArgb(255, 78, 154, 6);
            ls2.Color = OxyColors.Red;
            ls2.MarkerFill = OxyColors.Red;
            ls2.Title = "Iltmætnings data";

            ls2.Points.Add(new DataPoint(0, 5));
            ls2.Points.Add(new DataPoint(1, 3));
            ls2.Points.Add(new DataPoint(2, 10));
            ls2.Points.Add(new DataPoint(3, 7));
            ls2.Points.Add(new DataPoint(4, 1));
            ls2.Points.Add(new DataPoint(5, 2));
            ls2.Points.Add(new DataPoint(6, 8));
            ls2.Points.Add(new DataPoint(7, 12));
            ls2.Points.Add(new DataPoint(8, 30));
            ls2.Points.Add(new DataPoint(9, 8));

            PlotModel.Series.Add(ls2);
            //PlotModel.RefreshPlot(true);
        }

        private void FinishTrainingPassButton_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        
        }
    }
}
